/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_VEGA_MASS_SPRING_SYSTEM_H
#define SX_VEGA_MASS_SPRING_SYSTEM_H

#include <sxMassSpringSystem.h>
#include <sxMesh.h>
#include <sxTetrahedralMesh.h>
#include <sxDiscreteIntegrator.h>
#include <sxVega_ModifiableMassSpringSystem.h>

/* VegaFEM Includes */
#include <sparseMatrix.h>
#include <forceModel.h>

namespace Sx {
namespace Vega {
namespace Physics {

/*
 * VegaFEM provides double accuracy; therefore it is defined as a double-based
 * mass-spring system.
 */
class VegaMassSpringSystem : public Simulation::MassSpringSystem {
public:
	VegaMassSpringSystem();
	VegaMassSpringSystem(const VegaMassSpringSystem& massSpringSystem);
	virtual ~VegaMassSpringSystem();

	/* 
	 * Construct the Vega mass-spring system from an mesh that contains a
	 * set of quad faces (face-degree = 4).
	 */
	bool constructFromQuadMesh(const std::shared_ptr<Graphics::Objects::Mesh>& mesh, double surfaceDensity, double tensileStiffness, double shearStiffness, double bendStiffness, double damping);
	bool constructFromTetrahedralMesh(const std::shared_ptr<Graphics::Objects::TetrahedralMesh>& mesh, double density, double tensileStiffness, double damping);

	bool construct();
	void update(double dt);
	
	bool addNodeExternalForce(unsigned int index, Eigen::Vector3d& force);
	bool addNodeVelocity(unsigned int index, Eigen::Vector3d& velocity);
	bool addNodePosition(unsigned int index, Eigen::Vector3d& position);
	bool addNodeMass(unsigned int index, double mass);

	bool setNodeExternalForce(unsigned int index, Eigen::Vector3d& force);
	bool setNodeExternalForce(unsigned int index, double x, double y, double z);
	bool setNodeInternalForce(unsigned int index, Eigen::Vector3d& force);
	bool setNodeInternalForce(unsigned int index, double x, double y, double z);
	bool setNodeVelocity(unsigned int index, Eigen::Vector3d& velocity);
	bool setNodeVelocity(unsigned int index, double x, double y, double z);
	bool setNodePosition(unsigned int index, Eigen::Vector3d& position);
	bool setNodePosition(unsigned int index, double x, double y, double z);
	bool setNodePreviousPosition(unsigned int index, Eigen::Vector3d& position);
	bool setNodeAcceleration(unsigned int index, Eigen::Vector3d& acceleration);
	bool setNodeAcceleration(unsigned int index, double x, double y, double z);
	bool setNodeMass(unsigned int index, double mass);

	void zeroExternalForces();
	void setSubsteps(unsigned int substeps);
	bool reset();

	Eigen::Vector3d getNodeExternalForce(unsigned int index) const;
	Eigen::Vector3d getNodeAcceleraction(unsigned int index) const;
	Eigen::Vector3d getNodeVelocity(unsigned int index) const;
	Eigen::Vector3d getNodePosition(unsigned int index) const;
	double getNodeMass(unsigned int index) const;

protected:
	/* 
	 * Update node positions from integrator.
	 * POINTER_OWNERSHIP: YES
	 */
	bool updateVertexPositions();

	/* 
	 * VegaFEM Mass Spring System
	 * POINTER_OWNERSHIP: YES
	 */
	Vega_ModifiableMassSpringSystem* massSpringSystem;

	/*
	 * Mass Matrix
	 * POINTER_OWNERSHIP: YES
	 */
	SparseMatrix* massMatrix;

	/* 
	 * Discrete Integrator
	 * POINTER_OWNERSHIP: YES
	 */
	Sx::Physics::DiscreteIntegrator* integrator;

	/* 
	 * Force Model
	 * POINTER_OWNERSHIP: YES
	 */
	ForceModel* forceModel;
};

}

}

}

#endif
